package com.ywq.app.web.mvc.artRoom;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.biz.ArtPicBiz;
import com.ywq.app.biz.ArtRoomBiz;
import com.ywq.app.biz.AuxContentBiz;
import com.ywq.app.domain.ArtArtist;
import com.ywq.app.domain.ArtPic;
import com.ywq.app.domain.ArtRoom;
import com.ywq.app.domain.ArtRoomPic;
import com.ywq.app.domain.ArtRoomVideo;
import com.ywq.app.domain.Channel;
import com.ywq.app.domain.Content;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;

@Controller
public class ArtRoomController {
	private final static Logger logger = LoggerFactory.getLogger(ArtRoomController.class);
	@Autowired
	private ArtRoomBiz artRommBiz;
	@Autowired
	private ArtPicBiz artPicBiz;
	@Autowired
	private AuxContentBiz auxContentBiz;
	
	//展览馆首页
	@RequestMapping(value = "/artroom.htm", method = RequestMethod.GET)
	public String artRoom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			//轮播图roomTop
			List<Content> LbList = auxContentBiz.getRoomByChannel("roomTop", 6);
			//第二行
			List<Content> secList = auxContentBiz.getRoomByChannel("secRoom", 2);
			//第二行右
			List<Content> secRightList = auxContentBiz.getRoomByChannel("secRoomRight", 2);
			if(secRightList.size()>0){
			request.setAttribute("secRight", secRightList.get(0));
			}
			//右下
			List<Content> rightUpList = auxContentBiz.getRoomByChannel("rightUp", 2);
			//------
			//查询当前展览馆
			List<ArtRoom> nowRList =artRommBiz.getNowRoomList();
			if(nowRList.size()>0){
				request.setAttribute("nowR", nowRList.get(0));
				request.setAttribute("nowCount", nowRList.size());
			}
			List<ArtRoom> oldRList =artRommBiz.getOldRoomList();
			List<ArtRoom> oldSizeList =artRommBiz.getOldRoomListBySize(8);
			if(oldRList.size()>0){
				request.setAttribute("oldSizeList", oldSizeList);
				request.setAttribute("oldCount", oldRList.size());
			}
			//查询历史展览馆
			request.setAttribute("LbList", LbList);
			request.setAttribute("secList", secList);
			
			request.setAttribute("rightUpList", rightUpList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artRoom/artRoom";
		
	}
	//当前展览
	@RequestMapping(value = "/rnow.htm", method = RequestMethod.GET)
	public String rnow(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			List<ArtRoom> nowRList =artRommBiz.getNowRoomList();
			if(nowRList.size()>0){
				request.setAttribute("nList", nowRList);
			}
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artRoom/rnow";
		
	}
	//往期展览
	@RequestMapping(value = "/rback.htm", method = RequestMethod.GET)
	public String rback(Integer page,Integer y,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			PageModel modelOld = artRommBiz.getRBackList(page,y);
			Calendar cal = Calendar.getInstance();
		    int year = cal.get(Calendar.YEAR)+1;
		    List<Integer> years =new ArrayList<Integer>();
		    for(int i=0;i<15;i++){
		    	year=year-1;
		    	years.add(year);
		    }
		    request.setAttribute("years", years);
		    if(y!=null){
		    	request.setAttribute("y", y);
		    }
			request.setAttribute("modelOld", modelOld);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return "artOne/artRoom/rback";
		
	}
	//具体展览
	@RequestMapping(value = "/inroom.htm", method = RequestMethod.GET)
	public String rback(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			ArtRoom inroom = artRommBiz.getInRoomById(id);
			List<ArtRoomVideo>  artRoomVideoList = artRommBiz.getArtRoomVideoByRoomId(id);
			
			try {
				Date now =Calendar.getInstance().getTime();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date dateS = sdf.parse(inroom.getStartDate());
				Date dateE = sdf.parse(inroom.getEndDate());
				boolean isTrue =false;
				if(now.before(dateE) && now.after(dateS)){
					isTrue=true;
				}
				request.setAttribute("isTrue", isTrue);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			List<ArtArtist> istList=artRommBiz.getArtArtistList(id);
			List<Content> rightUpList = auxContentBiz.getRoomByChannel("rightUp", 2);
			request.setAttribute("istList", istList);
			request.setAttribute("inroom", inroom);
			request.setAttribute("rightUpList", rightUpList);
			request.setAttribute("artRoomVideoList", artRoomVideoList);
			
			
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artRoom/inroom";
		
	}
	//展览作品列表
	@RequestMapping(value = "/rpics.htm", method = RequestMethod.GET)
	public String rpics(String id,Integer page,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			ArtRoom inroom = artRommBiz.getInRoomById(id);
			if (page == null) {
				page = 1;
			}
			try {
				Date now =Calendar.getInstance().getTime();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date dateS = sdf.parse(inroom.getStartDate());
				Date dateE = sdf.parse(inroom.getEndDate());
				boolean isTrue =false;
				if(now.before(dateE) && now.after(dateS)){
					isTrue=true;
				}
				request.setAttribute("isTrue", isTrue);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			PageModel modelRpics = artRommBiz.getModelRpics(id,page);
			request.setAttribute("modelRpics", modelRpics);
			request.setAttribute("inroom", inroom);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artRoom/rpics";
		
	}
	//作品详情分页
	@RequestMapping(value = "/artpic.htm", method = RequestMethod.GET)
	public String artpic(String id,Integer page,String roomId,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			PageModel modelRpics = artRommBiz.getModelRpicsOne(roomId,page);
			request.setAttribute("modelRpics", modelRpics);
			request.setAttribute("roomId", roomId);
			if(page==1 && !"".equals(id) && id!=null){
				ArtPic artPic =artPicBiz.getArtPic(id);
				ArtRoomPic artRoomPic =new ArtRoomPic();
				artRoomPic.setArtPic(artPic);
				request.setAttribute("artPic", artRoomPic);
			}else{
				request.setAttribute("artPic", modelRpics.getList().get(0));
			}
			List<ArtRoom> nowRListl =artRommBiz.getNowRoomList();
			List<ArtRoom> nowList =new ArrayList<ArtRoom>();
			if(nowRListl.size()>4){
				nowList.add(nowRListl.get(0));
				nowList.add(nowRListl.get(1));
				nowList.add(nowRListl.get(2));
				nowList.add(nowRListl.get(3));
			}
			request.setAttribute("nowRList", nowList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artPic/artpic";
		
	}
	//作品详情
	@RequestMapping(value = "/apic.htm", method = RequestMethod.GET)
	public String apic(String id, String roomId,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			
			request.setAttribute("roomId", roomId);
			ArtPic artPic =artPicBiz.getArtPic(id);
			List<ArtRoom> nowRListl =artRommBiz.getNowRoomList();
			List<ArtRoom> nowList =new ArrayList<ArtRoom>();
			if(nowRListl.size()>4){
				nowList.add(nowRListl.get(0));
				nowList.add(nowRListl.get(1));
				nowList.add(nowRListl.get(2));
				nowList.add(nowRListl.get(3));
			}
			request.setAttribute("nowRList", nowList);
			request.setAttribute("artPic",artPic);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artPic/apic";
		
	}
	//广告
	@RequestMapping(value = "/comcon.htm", method = RequestMethod.GET)
	public String comcon(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			List<Content> rightUpList = auxContentBiz.getRoomByChannel("rightUp", 2);
			Content content =auxContentBiz.getContentId(id);
			request.setAttribute("content", content);
			request.setAttribute("rightUpList", rightUpList);
			request.setAttribute("comcon", "comcon");
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/common/comcon";
		
	}
	//首页底部
	@RequestMapping(value = "/show.htm", method = RequestMethod.GET)
	public String show(String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			List<Content> rightUpList = auxContentBiz.getRoomByChannel("rightUp", 2);
			Content content =auxContentBiz.getContentId(id);
			request.setAttribute("content", content);
			request.setAttribute("rightUpList", rightUpList);
			request.setAttribute("comcon", "show");
			//查出父类的名称
			Channel channel =auxContentBiz.getChannelById(content.getChannel());
			request.setAttribute("channelName", channel.getName());
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/common/comcon";
		
	}
	//视频页面
	@RequestMapping(value = "/flvList.htm", method = RequestMethod.GET)
	public String flvList(String roomid,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			List<Content> rightUpList = auxContentBiz.getRoomByChannel("rightUp", 2);
			request.setAttribute("rightUpList", rightUpList);
			List<ArtRoomVideo>  artRoomVideoList = artRommBiz.getAllArtRoomVideoByRoomId(roomid);
			request.setAttribute("artRoomVideoList", artRoomVideoList);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/artRoom/roomflv";
	}
}